﻿using System.Linq;
using System.Collections.Generic;
using OpenMVCRM.ViewModels;
using System.Collections.Generic;
using OpenMVCRM.Entities;
using OpenMVCRM.DataAccess;
namespace OpenMVCRM.Services
{
    public class ClientService
    {

        public static IEnumerable<IndustryViewModel> GetIndustries()
        {
            IRepositary repo = new Repositary();

            IList<IndustryViewModel> categoryVMList = new List<IndustryViewModel>();

            var industries = repo.GetAllIndustries().ToList();
            foreach (var item in industries)
            {
                categoryVMList.Add(new IndustryViewModel { ID = item.ID, Name = item.Name });
            }
            return categoryVMList;

        }

        public static ClientViewModel GetClientVM(Client client)
        {
            ClientViewModel clientVM = new ClientViewModel();
            if (client != null)
            {
                clientVM.ID = client.ID;
                clientVM.Name = client.Name;
                clientVM.SelectedIndustryId = client.IndustryID;
                clientVM.Description = client.Description;
                clientVM.WebSite = client.WebSite;
                clientVM.PrimaryPhone.Number = client.PrimaryPhoneNumber.Number;
                
            }

            return clientVM;

        }

        public static IEnumerable<ClientViewModel> GetClientVMs(IEnumerable<Client> clientEntities) 
        {
            IList<ClientViewModel> clientVMList=new List<ClientViewModel>();

            foreach (var clientEntity in clientEntities)
            {
                ClientViewModel client = new ClientViewModel();
                client.Name = clientEntity.Name;
                client.Description = clientEntity.Description;
                client.IndustryName = clientEntity.Industry.Name;
                client.WebSite = clientEntity.WebSite;
                client.ID = clientEntity.ID;

                clientVMList.Add(client);
            }


            return clientVMList;
        }

        public static IEnumerable<AddressViewModel> GetAddressVMs(IEnumerable<Address> addresseEntities)
        {
            IList<AddressViewModel> clientVMList = new List<AddressViewModel>();
            foreach (var addressEntity in addresseEntities)
            {
                AddressViewModel address = new AddressViewModel();
                address.AddressLine1 = addressEntity.AddressLine1;
                address.AddressLine2 = addressEntity.AddressLine2;
                address.City = addressEntity.City;
                address.ZipCode = addressEntity.ZipCode;
                address.ID = addressEntity.AddressID;
               //address.IsPrimary = addressEntity.IsPrimary;
                address.StateName = addressEntity.State.StateName;
                address.CountryName = addressEntity.Country.CountryName;

                clientVMList.Add(address);
            }
            return clientVMList;
        }

        public static AddressViewModel GetAddressVM(Address addressEntity)
        {
                AddressViewModel address = new AddressViewModel();
                address.AddressLine1 = addressEntity.AddressLine1;
                address.AddressLine2 = addressEntity.AddressLine2;
                address.City = addressEntity.City;
                address.ZipCode = addressEntity.ZipCode;
                address.ID = addressEntity.AddressID;
             //   address.IsPrimary = addressEntity.IsPrimary;
                return address;
        }
      

    }
}
